<?php


namespace app\admin\model;


use app\common\model\BaseModel;
use app\lib\exception\BaseException;

class Manager extends BaseModel
{
    //管理用户角色表
    public function role()
    {
        return $this->belongsTo('Role');
    }
    public function login()
    {
        $param = request()->param();
        // 验证用户是否存在
        $user = $this->isExists($this->filterUserData($param['username']));
        // 用户不存在
        if(!$user) throw new BaseException(['code'=>200,'msg'=>'用户不存在','errorCode'=>20000]);
        //用户是否被禁用
        $this->checkStatus($user->toArray());
        // 验证密码
        $this->checkPassword($param['password'],$user->password);
        // 登录成功 生成token，进行缓存，返回客户端
        return $this->CreateSaveToken($user->toArray(),'admin');
    }

    /**
     * 获取用户信息
     * @return array
     */
    public function getInfo()
    {
        $param = request()->param();
        //获取当前用户所有权限
        $data = $this->where('id',$param['id'])->with([
            'role'=>function($query){
            $query->with([
                'rules' => function($q){
                $q->order([
                    'order' => 'asc',
                    'id' => 'asc'
                ])->where('status',1);
                }
            ])->where('status',1);
            }
        ])->find()->toArray();
        $data['tree'] = [];
        // 规则名称，按钮级别显示
        $data['ruleNames'] = [];
        // 无限级分类
        $rules = [];
        if($data['role']){
            $rules = $data['role']['rules'];
            unset($data['role']['rules']);
        }
        // 超级管理员
        if($data['super'] == 1){
            $rules =  \app\admin\model\Rule::where('status',1)->order([
                'order'=>'asc',
                'id'=>'asc'
            ])->select()->toArray();
        }

        $data['tree'] = list_to_tree2($rules,'rule_id','child',0,function($item){
            return $item['menu'] == 1;
        });
        // 权限规则数组
        foreach ($rules as $v) {
            if($v['condition'] && $v['name']){
                $data['ruleNames'][] = $v['condition'].','.$v["method"];
            }
        }
        $result = [
    "id"=>$data["id"],
    "username"=>$data["username"],
    "avatar"=>$data["avatar"],
    "super"=>$data["super"],
    "role"=>$data["role"],
    "menus"=>$data["tree"],
    "ruleNames"=>$data["ruleNames"]
];
return $result;


    }

    public function getList(){
        $param = request()->param();
        $limit = intval(getValByKey('limit',$param,10));
        $page = intval(getValByKey('page',$param,1));
        $totalCount = $this->count();
        $where = [];
        if(array_key_exists('role_id',$param)) $where[] = ['role_id','=',$param['role_id']];
        if(array_key_exists('phone',$param)) $where[] = ['phone','=',$param['phone']];
        if(array_key_exists('nickname',$param)) $where[] = ['nickname','=',$param['nickname']];
                        $list = $this->where($where)->with(['role'=>function($q){
            $q->alias('a')->field('a.id,a.name');
        }])->page($page,$limit)->order([
            'id'=>'desc'
        ])->select()
        ->hidden(['password']);
        return [
            'list'=>$list,
            'totalCount'=>$totalCount
        ];
    }
}